package com.zxlspace.collection;

/**
 * hashmap实现高级版：linkedlist实现（数组+链表）
 */
import javax.sound.midi.Soundbank;
import java.util.LinkedList;

public class SxtMap002 {
    private LinkedList[] array = new LinkedList[1000];

    private int size;

    public int size() {
        return size;
    }

    public void put(Object key, Object value) {
        SxtEntry sxtEntry = new SxtEntry(key, value);
        int hash = key.hashCode();
        hash = hash < 0 ? -hash : hash; //hashcode 有可能为负数

        int a = hash % array.length;  //除以数组长度取余数,作为数组index,每个对象都有自己hashcode
        if (array[a] == null) {
            LinkedList list = new LinkedList();
            array[a] = list;
            list.add(sxtEntry);
            size++;
        }else {   //极小概率下hashcode取余会有相同的，所以需要链表来存储(不重复),然后再比较键值
            LinkedList list = array[a];
            for (int i=0; i<list.size(); i++) {
                SxtEntry e = (SxtEntry) list.get(i); //键值重复 直接覆盖
                if (e.key.equals(key)) {
                    e.value = value;
                }
            }
            list.add(sxtEntry);

        }
    }

    public Object get(Object key) {
        int a = key.hashCode() % array.length;
        if (array[a] != null) {
            LinkedList list = array[a];
            for (int i=0; i<list.size(); i++) {
                SxtEntry e = (SxtEntry) list.get(i);
                if (e.key.equals(key)) {
                    return e.value;
                }
            }
        }
        return null;
    }

    public void remove(Object key) {
        int a = key.hashCode() % array.length;
        if (null != array[a]) {
            LinkedList list = array[a];  //可能存有多个取余相同的key
            for (int i=0; i<list.size(); i++) {
                SxtEntry e = (SxtEntry) list.get(i);
                if (e.key.equals(key)) {
                    /*if (list.size() == 1) {
                        size--; //map底层数组
                    }*/
                    list.remove(key);

                }
            }

        }
    }

    public static void main(String[] args) {
        /*SxtMap002 map001 = new SxtMap002();
        map001.put("高崎", "张曼玉");
        map001.put("高崎1", "张曼玉1");
        map001.put("高崎1", "张曼玉2");
        map001.put("aaa", "vbbb");
        map001.put("ccc", "vbbb");

        Object object = map001.get("高崎1");
        System.out.println(object);
        System.out.println(map001.get("ccc"));*/

//        System.out.println( map001.containsKey("高崎3"));
//        map001.remove("aaa");
//        System.out.println(map001.size());

      /*  String a = new String("111");
        String b = new String("111");
        System.out.println(a == b);
        System.out.println(a.equals(b));*/


        //equals 相等,hashcode一定相等； 反之，hashcode相等，equals不一定相等
      /*  System.out.println(a.hashCode());
        System.out.println(b.hashCode());
        System.out.println(a.hashCode() == b.hashCode());*/

        Integer temp1 = new Integer(1234);
        Integer temp2 = new Integer(1234);
        System.out.println(temp1 == temp2);
        System.out.println(temp1.equals(temp2));

        System.out.println(temp1.hashCode());
        System.out.println(temp2.hashCode());
        System.out.println(temp1.hashCode() == temp2.hashCode());

        System.out.println(new SxtMap002().equals(new SxtMap002()));

    }
}

